package com.xhp.xtool.util.redis;

import com.xhp.xtool.domain.RedisDto;
import com.xhp.xtool.domain.RedisVo;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JedisUtil {

    /**
     * 测试连接
     *
     * @param redisDto
     * @return
     */
    public static boolean testConnect(RedisDto redisDto) {
        Jedis jedis = null;
        try {
            JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance(redisDto);
            jedis = jedisPool.getResource();
            String ping = jedis.ping();
            if ("PONG".equals(ping)) {
                return true;
            } else {
                throw new RuntimeException(ping);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JedisPoolUtil.returnResource(jedis);
        }
    }

    /**
     * 获取全部key
     *
     * @return
     */
    public static Set<String> keys(int index) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = JedisPoolUtil.getJedis();
            jedis.select(index);
            return jedis.keys("*");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JedisPoolUtil.returnResource(jedis);
        }
    }

    /**
     * 获取key的类型
     *
     * @param key
     * @return
     */
    public static String type(String key, int index) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = JedisPoolUtil.getJedis();
            jedis.select(index);
            res = jedis.type(key);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JedisPoolUtil.returnResource(jedis);
        }
        return res;
    }

    /**
     * 获取key的值
     *
     * @param key
     * @param type
     * @return
     */
    public static List<RedisVo> getValue(String key, String type, int index) {
        Jedis jedis = null;
        List<RedisVo> redisVoList = new ArrayList<>();
        try {
            jedis = JedisPoolUtil.getJedis();
            jedis.select(index);
            switch (type) {
                case "string":
                    redisVoList.add(new RedisVo(jedis.get(key)));
                    break;
                case "list":
                    List<String> valueList = jedis.lrange(key, 0, 100);
                    valueList.forEach(v -> redisVoList.add(new RedisVo(v)));
                    break;
                case "set":
                    List<String> valueSet = jedis.srandmember(key, 100);
                    valueSet.forEach(v -> redisVoList.add(new RedisVo(v)));
                    break;
                case "zset":
                    Set<String> valueZSet = jedis.zrange(key, 0, 100);
                    valueZSet.forEach(v -> redisVoList.add(new RedisVo(v)));
                    break;
                case "hash":
                    Map<String, String> valueMap = jedis.hgetAll(key);
                    valueMap.forEach((k, v) -> redisVoList.add(new RedisVo(k, v)));
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JedisPoolUtil.returnResource(jedis);
        }
        return redisVoList;
    }
}
